package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class Library {

    ArrayList<Book> bookList;
    ArrayList<Movie> movieList;
    UserAccountList userAccountsListObject;
    UserAccount currentUser;

    public Library()
    {
        //Initialisation of variables
        bookList=new ArrayList<Book>();
        addInitialBookToLibrary();
        movieList=new ArrayList<Movie>();
        addInitialMoviesToLibrary();
        userAccountsListObject=new UserAccountList();
        currentUser=null;
    }

    public void displayMessageToConsole(String displayString)
    {
        System.out.println(displayString);
    }

    public void addInitialMoviesToLibrary()
    {
        addMovieToLibrary("Sholey","Ramesh Sippy",1975,10);
        addMovieToLibrary("Josh","Mansoor Khan",2000,8);
        addMovieToLibrary("Hey Ram","Kaml Hasan",2000,9);
        addMovieToLibrary("Refugee","J. P. Datta",2000,7);
        addMovieToLibrary("Pukaar","Rajkumar Santoshi",2000,7);
        addMovieToLibrary("Dhobi Ghat","Kiran Rao",2011,4);
        addMovieToLibrary("F.A.L.T.U","Ramo D'souza",2011,6);
        addMovieToLibrary("Thank You","Annes Bazmee",2011,7);
        addMovieToLibrary("Hounted","Vikram Bhaat",2011,4);
        addMovieToLibrary("Ready","Annes Bazmee",2011,10);
        addMovieToLibrary("Table No 21","Aditya Datta",2013,5);
        addMovieToLibrary("Race 2","Abbas-Mastan",2013,5);
        addMovieToLibrary("Special 26","Neeraj Pandey",2013);
        addMovieToLibrary("Aurangzeb","Atul Sabharao",2013);
        addMovieToLibrary("Ranzanna","Annad Rai",2013);
    }

    public void addMovieToLibrary(String movieName,String directorName,int releaseYear,int rating)
    {
        Movie movieObject=new Movie(movieName,directorName,releaseYear,rating);
        movieList.add(movieObject);
    }

    public void addMovieToLibrary(String movieName,String directorName,int releaseYear)
    {
        Movie movieObject=new Movie(movieName,directorName,releaseYear);
        movieList.add(movieObject);
    }

    //Setting up Library for first time
    public void addInitialBookToLibrary()
    {
        addBookToLibrary("Database System Concepts","Korth",123456789,0);
        addBookToLibrary("Operating System Concepts","bach",234567891,10);
        addBookToLibrary("Java","Schildt",345678912,4);
        addBookToLibrary("Compiler","Sethi",456789123,15);
        addBookToLibrary("Data Structure","Forouzan",567891234,9);
    }

    public void addBookToLibrary(String bookName,String authorName,long ISBN,int availableCopies)
    {
        Book bookObject=new Book(bookName,authorName,ISBN,availableCopies);
        bookList.add(bookObject);
    }

    //Display list of all the books in the library
    public void displayBookList() {
        int loopCounter=0;

        //sorting booklist by its name
        Collections.sort(bookList, new BookComparatorByBookName());
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<93;loopCounter++)
            System.out.print("_");
        System.out.println();
        System.out.printf("%-30s %-30s %-10s %-20s\n","Name","Author","ISBN","Available Copies");
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<93;loopCounter++)
            System.out.print("_");
        System.out.println();
        for(loopCounter=0;loopCounter<bookList.size();loopCounter++)
        {
            System.out.printf("%-30s %-30s %-10s %-20s\n",bookList.get(loopCounter).bookName,bookList.get(loopCounter).authorName,bookList.get(loopCounter).ISBN,bookList.get(loopCounter).availableCopies);
        }
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<93;loopCounter++)
            System.out.print("_");
        System.out.println();
    }

    public void displayMovieList()
    {
        int loopCounter=0;

        //sorting movieList by its ReleaseYear
        Collections.sort(movieList, new MovieComparatorReleaseYear());
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<77;loopCounter++)
            System.out.print("_");
        System.out.println();
        System.out.printf("%-30s %-30s %-4s %-10s\n","Movie","Director","Year","Rating");
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<77;loopCounter++)
            System.out.print("_");
        System.out.println();
        for(loopCounter=0;loopCounter<movieList.size();loopCounter++)
        {
            System.out.printf("%-30s %-30s %-4d %-10s\n",movieList.get(loopCounter).movieName,movieList.get(loopCounter).directorName,movieList.get(loopCounter).releaseYear,movieList.get(loopCounter).displayMovieRating());
        }
        //Displaying horizontal line for UI purpose
        for(loopCounter=0;loopCounter<77;loopCounter++)
            System.out.print("_");
        System.out.println();
    }

    //search book having ISBN entered by customer
    public int searchBook(long customerResponseISBN)
    {
        //if found returns book index from booklist else returns -1
        int loopCounter=0;
        for(loopCounter=0;loopCounter<bookList.size();loopCounter++)
        {
            if(bookList.get(loopCounter).ISBN==customerResponseISBN)
                break;
        }
        if(loopCounter==bookList.size())
            return -1;
        else
            return loopCounter;
    }

    public long takeCustomerResponseISBN(){
        long customerResponseISBN;
        //Taking customer response
        InputStreamReader inputStreamReaderObject=new InputStreamReader(System.in);
        BufferedReader bufferedReaderObject=new BufferedReader(inputStreamReaderObject);
        try {
            customerResponseISBN=Long.parseLong(bufferedReaderObject.readLine());
        } catch (IOException e) {
            customerResponseISBN=0;
        }
        return customerResponseISBN;
    }

    public String takeStringInputFromCustomer()
    {
        String customerResponse;
        InputStreamReader inputStreamReaderObject=new InputStreamReader(System.in);
        BufferedReader bufferedReaderObject=new BufferedReader(inputStreamReaderObject);
        try {
            customerResponse=bufferedReaderObject.readLine();
        } catch (IOException e) {
        customerResponse="";
        }
        return customerResponse;
    }

    public String takeUserNameFromCustomer()
    {
        displayMessageToConsole("You need to login to use this feature\nUser Name : ");
        return takeStringInputFromCustomer();
    }

    public String takePasswordFromCustomer()
    {
        displayMessageToConsole("Password : ");
        return takeStringInputFromCustomer();
    }

    //Takes ISBN response from customer if book if available places order else display message
    //returns true if your order is placed else returns false
    public boolean placeOrder() {
        boolean orderStatus;
        System.out.println("\n\n\tEnter ISBN of book you want to borrow : ");
        long customerResponseISBN=takeCustomerResponseISBN();

        //search availability of required book on the basis of ISBN
        int bookIndex=searchBook(customerResponseISBN);
        if(bookIndex==-1)
        {
            System.out.println("\tSorry we don't have that book yet");
            orderStatus=false;
        }
        else
        {
            if(bookList.get(bookIndex).availableCopies>0)
            {
                bookList.get(bookIndex).availableCopies--;
                orderStatus=true;
                System.out.println("\tThank You! Enjoy the book");
            }
            else
            {
                orderStatus=false;
                System.out.println("\tSorry we don't have available copy of that book");
            }
        }
        return orderStatus;
    }

    public boolean checkLoggedInCurrentUser()
    {
        if(currentUser==null)
            return false;
        else
            return true;
    }

    public boolean userLogIn()
    {
        String userName=takeUserNameFromCustomer();
        String password=takePasswordFromCustomer();
        currentUser=userAccountsListObject.userAccountLoginForm(userName,password);
        return checkLoggedInCurrentUser();
    }
}
